package com.stx.test.day0911;

import java.util.Arrays;

/**
 * packageName com.stx.day0911
 *
 * @author YangYi
 * @className selectSort
 * @date 2025/9/10
 * @description TODO
 */
public class SelectSort {
    public static void main(String[] args) {
        // 选择排序
        int[] arrays = {12,5,3,56,9,45,2,6,8,47};
        System.out.println(Arrays.toString(arrays));
        selectSortMethod(arrays);
    }
    /**
     * 选择排序
     * 每次从待排序的数据中选择最小（或最大）的元素，
     * 放到已排序序列的末尾，直到全部数据排序完成。
     * @param arrays
     */
    public static void selectSortMethod(int[] arrays){
        for (int i = 0; i < arrays.length-1; i++) {
            int max = i;
            for (int j = i + 1; j < arrays.length; j++) {
                if(arrays[max] < arrays[j]){
                    max = j;
                }
            }
            if(max != i){
                int temp = arrays[i];
                arrays[i] = arrays[max];
                arrays[max] = temp;
            }
        }
        System.out.println(Arrays.toString(arrays));
    }
}
